//
//  InviteFirendsViewController.m
//  NiuBanJinGold
//
//  Created by zf on 15/10/15.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "InviteFirendsViewController.h"
#import "Friend.h"

@interface InviteFirendsViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (nonatomic , strong) NSMutableArray *array_data;
@end

@implementation InviteFirendsViewController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self getNetData];
}
-(void)getNetData {
    
   [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary *dic_myInvitat = @{@"userId" : GETCURRENTUSERINOBYPRAMETERES(user_userId) , @"token" : GETCURRENTUSERINOBYPRAMETERES(user_userToken)};
    [[NETWorkingUtil sharedNETWorking] zzjrMore_myInvitationByParmeteres:dic_myInvitat  transferResultUsingBlock:^(id parsedData, NSError *error) {
        
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (!error) {
            
            if ([parsedData[@"status"] integerValue] == 0) {
                NSDictionary *dic_result = parsedData[@"result"];
//                NSString *userName = parsedData[@"userName"];
                NSString *invitationCode = dic_result[@"invitationCode"];
                NSString *invitationRule = dic_result[@"invitationRule"];
                
                NSArray *arr_list = dic_result[@"list"];
                if (invitationCode.length != 0 ) {
                    
                    UILabel *label_invireCode = (UILabel *)[self.view viewWithTag:200];
                    label_invireCode.text = invitationCode;
                }
                if (invitationRule.length != 0) {
                    UIView *view_rule = [self.view viewWithTag:150];
                    UILabel *label_rule = [view_rule viewWithTag:201];
                    label_rule.text = invitationRule;
                }
                UITableView *tableView = (UITableView *)[self.view viewWithTag:700];
                if ([arr_list count] == 0) {
                    EmptyDataView *view_empty = [[EmptyDataView alloc]initWithFrame:tableView.frame];
                    [self.view addSubview:view_empty]; // 加过一次后就永远存在了，要想办法去掉
                }else {
                    
                    [self.view bringSubviewToFront:tableView]; //把tableview放前面就行了，不用remove再加,省资源
                }

                for (NSDictionary *dic in arr_list) {
                    
                    Friend *friend = [[Friend alloc]initWithDic:dic];
                    [self.array_data addObject:friend];
                    UITableView *tableView = (UITableView *)[self.view viewWithTag:700];
                    [tableView reloadData];
                }
            }else {
                
                [UsefulUtil showAlertView:parsedData[@"msg"]];
            }
        }else {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [UsefulUtil showAlertView:@"遇到网络问题,请稍后重试!"];
        }
    }];
}
#pragma setValueForLabelByData

-(void)setValueForLabelByData {
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"邀请好友";
    
    UIImageView *imgView = [[UIImageView alloc]init];
    imgView.image = [UIImage imageNamed:@"beef.png"];
    
    
    UILabel *label_userName = [[UILabel alloc]init];
    label_userName.text = @"用户名:";
     label_userName.font = [UIFont boldSystemFontOfSize:15.0];
    UILabel *label_dataUserName = [[UILabel alloc]init];
    label_userName.adjustsFontSizeToFitWidth = YES;
    label_userName.textColor = [UsefulUtil colorWithHexString:@"#989898"];
    label_dataUserName.text = GETCURRENTUSERINOBYPRAMETERES(user_userName) ;
    label_dataUserName.adjustsFontSizeToFitWidth = YES;
    label_dataUserName.font = (iPhone4 || iPhone5 )?[UIFont boldSystemFontOfSize:13.0]:[UIFont boldSystemFontOfSize:15.0];
    label_dataUserName.textColor = [UsefulUtil colorWithHexString:@"#434343"];
    UILabel *lael_inviteCode = [[UILabel alloc]init];
    lael_inviteCode.text = @"我的邀请码:";
    lael_inviteCode.adjustsFontSizeToFitWidth = YES;
    lael_inviteCode.font = (iPhone4 || iPhone5 )?[UIFont boldSystemFontOfSize:13.0]:[UIFont boldSystemFontOfSize:15.0];
    lael_inviteCode.textColor = [UsefulUtil colorWithHexString:@"#989898"];
    UILabel *label_dataInviteCode = [[UILabel alloc]init];
    label_dataInviteCode.text = GETCURRENTUSERINOBYPRAMETERES(user_userName);
    label_dataInviteCode.tag = 200;

    label_dataInviteCode.adjustsLetterSpacingToFitWidth = YES;
    label_dataInviteCode.font =  (iPhone4 || iPhone5 )?[UIFont boldSystemFontOfSize:13.0]:[UIFont boldSystemFontOfSize:15.0];
    label_dataInviteCode.textColor = [UsefulUtil colorWithHexString:@"#434343"];
    label_dataInviteCode.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:imgView];
    [self.view addSubview:label_userName];
    [self.view addSubview:label_dataUserName];
    [self.view addSubview:label_dataInviteCode];
    [self.view addSubview:lael_inviteCode];
    
    imgView.frame = CGRectMake( MULTIPLYWIDTH(15), MULTIPLYHEIGHT(16), MULTIPLYWIDTH(73), MULTIPLYWIDTH(73));
    label_userName.frame = CGRectMake(CGRectGetMaxX(imgView.frame) + MULTIPLYWIDTH(18), MULTIPLYHEIGHT(36), MULTIPLYWIDTH(100), MULTIPLYHEIGHT(15));
    label_dataUserName.frame = CGRectMake(CGRectGetMaxX(label_userName.frame) + MULTIPLYWIDTH(11), CGRectGetMinY(label_userName.frame), MULTIPLYWIDTH(150), MULTIPLYWIDTH(20.0));
    lael_inviteCode.frame = CGRectMake(CGRectGetMinX(label_userName.frame), CGRectGetMaxY(label_userName.frame) + MULTIPLYHEIGHT(10), MULTIPLYWIDTH(100), MULTIPLYHEIGHT(20.0));
    label_dataInviteCode.frame = CGRectMake(CGRectGetMinX(label_dataUserName.frame), CGRectGetMinY(lael_inviteCode.frame), MULTIPLYWIDTH(80), MULTIPLYHEIGHT(17));
    
    //复制按钮
    UIButton *button_copy = [UsefulUtil buttonCreationWithFrame:CGRectMake(CGRectGetMaxX(label_dataInviteCode.frame) + MULTIPLYWIDTH(1.0), label_dataInviteCode.frame.origin.y - MULTIPLYHEIGHT(2.0/2), MULTIPLYWIDTH(66.0), MULTIPLYHEIGHT(20.0)) textColor:[UsefulUtil colorWithHexString:@"#336699"] fontName:[UIFont systemFontOfSize:15.0] text:@"复制"];

    [button_copy addTarget:self action:@selector(copy:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button_copy];
    
    
    UIView *view_rule = [[UIView alloc]initWithFrame:CGRectMake( 0 , CGRectGetMaxY(imgView.frame) + MULTIPLYHEIGHT(16), self.view.frame.size.width, MULTIPLYHEIGHT(65))];
    view_rule.tag = 150;
    view_rule.backgroundColor = [UsefulUtil colorWithHexString:@"#ededed"];
    [self.view addSubview:view_rule];
    
    
    
    UIImageView *image_invite = [[UIImageView alloc]init];
   
    image_invite.image = [UIImage imageNamed:@"icon-@2x.png"];
    UILabel *label_ruleTitle = [[UILabel alloc]init];
    label_ruleTitle.text = @"邀请规则:";
    label_ruleTitle.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:15];
    label_ruleTitle.adjustsFontSizeToFitWidth = YES;
    label_ruleTitle.textColor = [UIColor redColor];
    UILabel *labeel_rule = [[UILabel alloc]init];
    
    labeel_rule.text = @"邀请用户成功,即可获得200积分,多邀多得。";
    labeel_rule.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:15];
    labeel_rule.adjustsFontSizeToFitWidth = YES;
    labeel_rule.tag = 201;
    labeel_rule.textColor = [UsefulUtil colorWithHexString:@"#434343"];
    labeel_rule.numberOfLines = 0;
    
//    UILabel *labeel_rule1 = [[UILabel alloc]init];
    
//    labeel_rule1.text = @"多邀多得。";
//    labeel_rule1.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:15];
//    labeel_rule1.adjustsFontSizeToFitWidth = YES;
    [view_rule addSubview:image_invite];
    [view_rule addSubview:label_ruleTitle];
    [view_rule addSubview:labeel_rule];
//    [view_rule addSubview:labeel_rule1];
    image_invite.frame = CGRectMake(MULTIPLYWIDTH(15), CGRectGetMinX(view_rule.frame) + MULTIPLYHEIGHT(14), MULTIPLYWIDTH(10), MULTIPLYHEIGHT(15));
    label_ruleTitle.frame = CGRectMake(CGRectGetMaxX(image_invite.frame) + MULTIPLYWIDTH(4), CGRectGetMinX(view_rule.frame) + MULTIPLYHEIGHT(14), MULTIPLYWIDTH(60), MULTIPLYHEIGHT(15));
    labeel_rule.frame = CGRectMake(CGRectGetMaxX(label_ruleTitle.frame), CGRectGetMinY(label_ruleTitle.frame), MULTIPLYWIDTH(300), MULTIPLYHEIGHT(17));
//    labeel_rule1.frame = CGRectMake(CGRectGetMaxX(label_ruleTitle.frame), CGRectGetMaxY(labeel_rule.frame) + MULTIPLYHEIGHT(5), MULTIPLYWIDTH(70), MULTIPLYHEIGHT(15));
    
    UILabel *lebel_regcord = [[UILabel alloc]init];
    lebel_regcord.text = @"邀请记录";
    lebel_regcord.textColor = REDCOLOR;
    lebel_regcord.textAlignment = NSTextAlignmentCenter;
    lebel_regcord.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:15];
    lebel_regcord.adjustsFontSizeToFitWidth = YES;
    lebel_regcord.frame = CGRectMake(0, CGRectGetMaxY(view_rule.frame), self.view.frame.size.width, MULTIPLYHEIGHT(44));
    [self.view addSubview:lebel_regcord];
    
    self.array_data = [NSMutableArray array];
    UITableView *tableView = [[UITableView alloc]init];
    tableView.tag = 700;
    tableView.frame = CGRectMake( 0, CGRectGetMaxY(lebel_regcord.frame), self.view.frame.size.width, 450);
    [self.view addSubview:tableView];
    UIView *view_footer = [[UIView alloc]initWithFrame:CGRectZero];
    [tableView setTableFooterView:view_footer];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView setSeparatorColor:[UsefulUtil colorWithHexString:@"#c8c7cc"]];
    
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction)];
    [self.view addGestureRecognizer:swipeGesture];
    
}
- (void)copy:(id)sender {
    UILabel *label = (UILabel *)[self.view viewWithTag:200];
    UIPasteboard *pastboard = [UIPasteboard generalPasteboard];
    pastboard.string = label.text;
    
    if (pastboard.string) {
        
        [UsefulUtil showAlertView:@"邀请码已复制!"];
    }
}

#pragma mark -swipeGestureAction
-(void)swipeGestureAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view_header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, MULTIPLYHEIGHT(1.0 / 2))];
    view_header.backgroundColor = [UIColor redColor];
    return view_header;
}
#pragma mark-heightForHeaderInSection
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return MULTIPLYHEIGHT(2.0 / 2);
}
#pragma mark-umberOfSectionsInTableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -numberOfRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array_data.count;
}
#pragma mark-cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellId = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (nil == cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    Friend *friend = self.array_data[indexPath.row];
    
    cell.textLabel.text = friend.mobile;
    cell.textLabel.textColor = [UsefulUtil colorWithHexString:@"#434343"];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    cell.detailTextLabel.text = friend.time;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
    cell.detailTextLabel.textColor = [UsefulUtil colorWithHexString:@"#989898"];
    UILabel *label = [[UILabel alloc] init]; //定义一个在cell最右边显示的label
    NSString *reward = friend.reward;
    NSString *str_reward  = [NSString stringWithFormat:@"积分 + %@" , reward];

    label.textColor = REDCOLOR;
    [label setText:str_reward];
    label.font = [UIFont boldSystemFontOfSize:15];
    
    [label sizeToFit];
    
    label.backgroundColor = [UIColor clearColor];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        label.frame =CGRectMake(SCREEN_WIDTH - label.frame.size.width - 10,\
                                12, label.frame.size.width, label.frame.size.height);
    } else {
        label.frame =CGRectMake(SCREEN_WIDTH - label.frame.size.width - 20,\
                                12, label.frame.size.width, label.frame.size.height);
    }
    [cell.contentView addSubview:label];
    label.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return MULTIPLYHEIGHT(90.0/2);
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
